<?php
class IndexAction extends CommonAction {
	
	/*
	 * OA主框架页面
	 */
    public function index(){  
	   
	   $this->display();
    }
		
	/*
	 * OA顶部页面
	 */
	public function top(){  
		$this->assign("now_time",time());  //获取当前时间从前台显示
		
		$company = M("company");
		$c_info = $company->field("c_site_url")->find();
		$this->assign("site_url",$c_info['c_site_url']);
	    $this->display();	
	}	
	
	/*
	 * OA左侧导航页面
	 */
	public function menu(){ 
	   $Uid = $_SESSION['UserId'];
	   $permissions = M("permissions");
	   $p_info = $permissions->where("p_uid = {$Uid}")->order("p_column_id asc")->select();

	   for($i=0;$i<count($p_info);$i++){
		   $column[$p_info[$i]['p_column_id']] = $p_info[$i]['p_column_enable'];   
		   $column_son[] = $p_info[$i]['p_column_son'];
	   }
	   $this->assign("column",$column);
	   $this->assign("son_1",explode(",",$column_son[0]));
	   $this->assign("son_2",explode(",",$column_son[1]));
	   $this->assign("son_3",explode(",",$column_son[2]));
	   $this->assign("son_4",explode(",",$column_son[3]));
	   $this->assign("son_5",explode(",",$column_son[4]));
	   $this->assign("son_6",explode(",",$column_son[5]));
	   $this->assign("son_7",explode(",",$column_son[6]));
	   $this->assign("son_8",explode(",",$column_son[7]));
	   $this->display();	
	}
	
	/*
	 * OA主内容页面
	 */
	public function main(){
		//登陆成功后根据ID获取相应的用户信息
		$Uid  = session('UserId');
		$this->Online($_SESSION['UserId']);
		$user = M('employees'); 
		//多表查询，同时查询用户信息表，部门表，职位表
		$sql = "select * from oa_department as d,oa_employees as e,oa_position as p where (e.u_department = d.d_id or e.u_department = 0) and e.u_position = p.p_id and e.u_id = {$Uid}";
		$user_info = $user->query($sql);
		$this->assign("user_info",$user_info);		
		/*查询所属该用户的公告通知*/
		//查出用户所属部门ID
			$user   = M("employees");			
			$u_info = $user->field("u_department")->find($Uid);
			
			$notice = M("notice");
			$department = M("department");
			
			//分页处理部分
			$count = $notice->where("(n_target = 2 or n_department = {$u_info['u_department']}) and n_rUid != {$Uid}")->count();
			
			//查询出本部门所发公告或者所有员工接收的公告(排除自身所发公告)
			$n_info = $notice->where("(n_target = 2 or n_department = {$u_info['u_department']}) and n_rUid != {$Uid}")->limit(7)->select();
			
			//通过内部循环将部门ID换成部门名称
			for($i=0;$i<count($n_info);$i++){
				$d_info = $department->where("d_id = {$n_info[$i]['n_department']}")->select();
				$n_info[$i]['n_department'] = $d_info[0]['d_name'];
			} 			
			if(empty($n_info)){
			   $this->assign("n","2");	
			}else{
			   $this->assign("n","1");	
			}
			$this->assign("n_info",$n_info);
			
			/*查询出公共聊天室信息*/
			$chatroom = M("chatroom");
			$c_info = $chatroom->limit(8)->select();
			if(empty($c_info)){
			   $this->assign("c","2");	
			}else{
			   $this->assign("c","1");	
			}
			$this->assign("c_info",$c_info);
			
			/*调取员工的工作日志*/
			$joblog = M("joblog");
			$j_info = $joblog->where("j_uid = {$Uid}")->select();
			$this->assign("j_info",$j_info);
			
			/*调取个人的办公桌布局*/
			if($user_info[0]['u_window']==1){
			   $absolute = M("absolute");
			   $ab_info = $absolute->where("a_uid = {$Uid}")->order("a_sid asc")->select();
			   $this->assign("ab_info",$ab_info);
			}
			
			/*查询拖动的版块ID*/
			$section = M("section");
			$section_info = $section->select();
			$this->assign("section_info",$section_info);
			
			/*调取个人文件柜*/
			$filecabinet = M("filecabinet");
			$f_info = $filecabinet->where("f_uid = {$Uid}")->limit(5)->select();
			$this->assign("f_info",$f_info);
			
			/*获取系统当前时间*/
			$this->assign("now_time",time());
			//获取当天日期
			$this->assign("now_day",date("Y-m-d",time()));
			
			/*获取常用网址*/
			$url = M("url");
			$u_info = $url->where("u_uid = {$Uid}")->order("u_number asc")->select();
			$this->assign("u_info",$u_info);
			
			/*获取公司当天的紧急通知*/
			$jinji = M("jinji");
			$jinji_date = date("Y-m-d",time());
			$j_info = $jinji->select();
			for($j=0;$j<count($j_info);$j++){
			    $j_time = date("Y-m-d",$j_info[$j]['j_starttime']);
				if($jinji_date == $j_time){
				   $this->assign("jinji_info",$j_info[$j]);
				}
			}
			
			//获取到会议信息
			$meet = M("meetingnotice");
			$sql2 = "select * from oa_meeting as m,oa_meetingnotice as mn where m.m_id = mn.n_mid and (mn.n_department = {$user_info[0]['u_department']} or mn.n_uid = {$Uid}) order by m.m_starttime desc limit 0,7";
			$meet_info = $meet->query($sql2);
			$this->assign("meet_info",$meet_info);
			
			//获取到公司基本信息
			$company = M("company");
			$company_info = $company->select();
			$this->assign("company_info",$company_info);
			
			//获取到投票信息
			$vote = M("voteinfo");
			$now_t = time();
			$v_info = $vote->where("v_display = 1 and v_endtime > {$now_t}")->order("v_id desc")->limit(4)->select();
			$this->assign("v_info",$v_info);
	        $this->display();	
	}
	
    //主页面AJAX处理部分
	public function main_ajax(){
	   $Uid = $_SESSION['UserId'];
	   $nowTime = $_GET['nowTimes'];
	   $memo = M("memo");
	   $m_info = $memo->field("m_msg")->where("m_time = '{$nowTime}' and m_uid = {$Uid}")->find();	
	   if(empty($m_info)){
		  echo "当天没有设置备忘录信息..."; 
	   }else{
		  echo $m_info['m_msg'];  
	   }
	}
	
	//添加备忘按钮点击后处理
	public function add_memo(){
		$Uid = $_SESSION['UserId'];
		$m_time = $_GET['d_times'];
		$m_msg  = $_GET['value'];
		$memo = M("memo");
		$data['m_uid'] = $Uid;
		$data['m_time'] = $m_time;
		$data['m_msg'] = $m_msg;
		$m_sum = $memo->where("m_time = '{$data['m_time']}'")->count();
		if($m_sum>0){
		   echo 3;	
		}
		
		if($memo->add($data)){
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"添加备忘录",1);
		   echo 1;	
		}else{
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"添加备忘录",0);
		   echo 2;	
		}
	}
	
	//更新备忘录处理
	public function update_memo(){
		$Uid = $_SESSION['UserId'];
		$m_time = $_GET['d_times'];
		$m_msg  = $_GET['value'];
		$memo = M("memo");
		
		$data['m_msg'] = $m_msg;
		if($memo->where("m_time = '{$m_time}'")->save($data)){
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"更新备忘录",1);
		   echo 1;	
		}else{
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"更新备忘录",0);
		   echo 2;	
		}
	}
	
	//增加常用网站地址处理
	public function add_url(){
	    $u_url = $_GET['u_url'];
		$u_number = $_GET['u_number'];	
		
		$url = M("url");
		$data['u_url'] = $u_url;
		if($url->where("u_number = {$u_number}")->save($data)){
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"增加常用网站地址",1);
		   echo 1;	
		}else{
		   $this->Online($_SESSION['UserId']);
		   $this->Log($_SESSION['UserId'],"增加常用网站地址",0);
		   echo 2;	
		}
	}
	
	//办公桌拖动位置保存处理程序
	public function save_absolute(){
	   $Uid = $_SESSION['UserId'];
	   $data['a_left'] = $_GET['x'];
	   $data['a_top'] = $_GET['y'];
	   $data['a_zIndex'] = $_GET['z'];
	   $Sid =  $_GET['sid'];   
	   
	   $absolute = M("absolute");
	   
	   if($absolute->where("a_uid = {$Uid} and a_sid = {$Sid}")->save($data)){
		  echo 1;   
	   }else{
		  echo 2;   
	   }
	}
	
	//统计在线人数AJAX处理
	public function online_num(){
	   $Uid = $_SESSION['UserId'];
	   
	   $now_time = time();
	   $online = M("online");
	   $o_info = $online->where("o_uid = {$Uid}")->find();
	   
	   $new_time = ($now_time - $o_info['o_time']);
	   $minutes = intval($new_time/60);
	   
	   //判断多久不在线将删除在线记录
	   if($minutes>30){
		  if(file_exists("./Public/online_employees/{$Uid}.txt")){
		     unlink("./Public/online_employees/{$Uid}.txt"); 
		  }
	   }else{
		  if(!file_exists("./Public/online_employees/{$Uid}.txt")){
		     touch("./Public/online_employees/{$Uid}.txt");   
		  }
	   }  
	   
	   //循环查询现在人数
	   $num = array();
	   $dir = dir("./Public/online_employees");
	   while(false != ($file=$dir->read())){
		     if($file!='.'&&$file!='..'){
				$num[] = $file;  
			 }
	   }
	   $dir->close();
	   echo count($num);
	}
	
	//更换个人照片
	public function img_head(){
	   $this->Online($_SESSION['UserId']);
	   $this->display();	
	}
}